package com.czz.manage.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.manage.entity.Question;
import com.czz.manage.query.QuestionQuery;
import com.czz.manage.service.QuestionService;
import com.czz.manage.vo.QuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: QuestionAdminController
 * @Author Czz
 * @Date 2020/4/27
 * @Time 18:26
 **/
@RestController
@RequestMapping(value = "/admin/questions")
public class QuestionAdminController {

    @Autowired
    QuestionService questionService;

    /**
    * @Description 分页查询问答列表
    * @Param [page, limit, query]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/4/27
    * @Time 19:58
    **/
    @GetMapping(value = "{page}/{limit}")
    public R getQuestionPageList(@PathVariable Long page, @PathVariable Long limit, QuestionQuery query){
        if (page <= 0 || limit <= 0){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<Question> pageParam = new Page<>(page, limit);
        questionService.getQuestionPageList(pageParam,query);
        List<Question> records = pageParam.getRecords();
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
    * @Description 根据id删除问答及其回复
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/4/27
    * @Time 20:47
    **/
    @DeleteMapping(value = "{id}")
    public R deleteQuestion(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        questionService.deleteQuestionById(id);
        return R.ok();
    }


    /**
    * @Description 根据ID查询问答详情
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/4/27
    * @Time 21:31
    **/
    @GetMapping(value = "{id}")
    public R getQuestionById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        QuestionVo questionVo = questionService.getQuestionById(id);
        return R.ok().data("item",questionVo);
    }

    /**
    * @Description 根据id修改问答
    * @Param [id, questionVo]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/4/27
    * @Time 22:00
    **/
    @PutMapping(value = "{id}")
    public R updateQuestionById(@PathVariable String id,QuestionVo questionVo){
        if (StringUtils.isEmpty(id) || ObjectUtils.isEmpty(questionVo)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        questionService.updateQuestionById(id,questionVo);
        return R.ok();
    }
}
